www.gusucode.com > vc++ 远程控制示例源程序-源码程序 > vc++ 远程控制示例源程序-源码程序\code\PeerYouC\PeerFileRemoteListView.cpp
//Download by http://www.NewXing.com // PeerFileRemoteListView.cpp : implementation file // #include "stdafx.h" #include "peeryouc.h" #include "PeerFileRemoteListView.h" #include "peersocket.h" #include "command.h" #include "createdirdlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CPeerFileRemoteListView IMPLEMENT_DYNCREATE(CPeerFileRemoteListView, CView) CPeerFileRemoteListView::CPeerFileRemoteListView() { } CPeerFileRemoteListView::~CPeerFileRemoteListView() { } BEGIN_MESSAGE_MAP(CPeerFileRemoteListView, CView) //{{AFX_MSG_MAP(CPeerFileRemoteListView) ON_WM_CREATE() ON_COMMAND(ID_PEER_FILE_REMOTE_LIST, OnPeerFileRemoteList) ON_COMMAND(ID_PEER_FILE_REOMTE_BIG, OnPeerFileReomteBig) ON_COMMAND(ID_PEER_FILE_REOMTE_REPORT, OnPeerFileReomteReport) ON_COMMAND(ID_PEER_FILE_REOMTE_SMAll, OnPEERFILEREOMTESMAll) ON_WM_SIZE() ON_CBN_SELENDOK(ID_PEER_FILE_REMOTE_DROPLIST,OnSelectChange) ON_NOTIFY(NM_DBLCLK,1001, OnRemoteLD ) ON_NOTIFY(NM_RCLICK,1001, OnLocalRC ) ON_NOTIFY(LVN_BEGINDRAG,1001, OnBeginDrag) ON_COMMAND(IDR_FILE_REMOTE_CREATEDIR, OnFileRemoteCreatedir) ON_COMMAND(IDR_FILE_REMOTE_DELFILE, OnFileRemoteDelfile) ON_COMMAND(IDR_FILE_REMOTE_EXE, OnFileRemoteExe) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPeerFileRemoteListView drawing void CPeerFileRemoteListView::OnDraw(CDC* pDC) { CDocument* pDoc = GetDocument(); // TODO: add draw code here } ///////////////////////////////////////////////////////////////////////////// // CPeerFileRemoteListView diagnostics #ifdef _DEBUG void CPeerFileRemoteListView::AssertValid() const { CView::AssertValid(); } void CPeerFileRemoteListView::Dump(CDumpContext& dc) const { CView::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CPeerFileRemoteListView message handlers int CPeerFileRemoteListView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here if(!list.Create( WS_VISIBLE|WS_CHILD,CRect(0,0,0,0),this,1001)) return -1; list.ModifyStyle(0,LVS_REPORT); style=LVS_REPORT; LVCOLUMN colinfo; memset(&colinfo,0,sizeof(colinfo)); colinfo.mask=LVCF_TEXT|LVCF_WIDTH; colinfo.cx=100; colinfo.cchTextMax=255; colinfo.pszText="名称"; colinfo.iSubItem=0; colinfo.iOrder=0; list.InsertColumn(0,&colinfo); colinfo.pszText="类型"; colinfo.iSubItem=1; colinfo.iOrder=1; list.InsertColumn(1,&colinfo); colinfo.pszText="大小"; colinfo.iSubItem=2; colinfo.iOrder=2; list.InsertColumn(2,&colinfo); colinfo.pszText="修改时间"; colinfo.iSubItem=3; colinfo.iOrder=3; list.InsertColumn(3,&colinfo); imagelists.Create(16,16,ILC_MASK,16,1); imagelistb.Create(32,32,ILC_MASK,16,1); imagelists.Add(AfxGetApp()->LoadIcon(IDI_DIR)); imagelistb.Add(AfxGetApp()->LoadIcon(IDI_DIR)); list.SetImageList(&imagelists,LVSIL_SMALL); list.SetImageList(&imagelistb,LVSIL_NORMAL); list.Register(); if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP |CBRS_TOOLTIPS | CBRS_FLYBY /*| CBRS_SIZE_DYNAMIC*/) || !m_wndToolBar.LoadToolBar(IDR_PEER_FILE_REMOTE)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } AddTooBarButton(); if(!image.Create(16,16,ILC_MASK,4,1)) return -1; image.Add(AfxGetApp()->LoadIcon(IDI_DRIVER_A)); image.Add(AfxGetApp()->LoadIcon(IDI_DRIVER_FIX)); image.Add(AfxGetApp()->LoadIcon(IDI_DRIVER_CD)); image.Add(AfxGetApp()->LoadIcon(IDI_DRIVER_NET)); m_wndToolBar.m_driver.SetImageList(&image); return 0; } void CPeerFileRemoteListView::AddTooBarButton() { #define COMBO_BOX_WIDTH 80 int index = 0; CRect rect; while(m_wndToolBar.GetItemID(index)!= ID_PEER_FILE_REMOTE_STATIC) index++; m_wndToolBar.SetButtonInfo(index, ID_PEER_FILE_REMOTE_STATIC,TBBS_SEPARATOR,110); m_wndToolBar.GetItemRect(index, &rect); rect.top+=4; // rect.bottom += 2; // then .Create the combo box and show it if (!m_wndToolBar.m_title.Create( "Remote Machine", WS_CHILD|WS_VISIBLE , rect, &m_wndToolBar, ID_PEER_FILE_REMOTE_STATIC)) { TRACE0("Failed to create combo-box\n"); return ; } index = 0; while(m_wndToolBar.GetItemID(index)!= ID_PEER_FILE_REMOTE_DROPLIST) index++; m_wndToolBar.SetButtonInfo(index, ID_PEER_FILE_REMOTE_DROPLIST,TBBS_SEPARATOR,COMBO_BOX_WIDTH); m_wndToolBar.GetItemRect(index, &rect); rect.top+=2; rect.bottom += 200; // then .Create the combo box and show it if (!m_wndToolBar.m_driver.Create( WS_CHILD|WS_VISIBLE | CBS_AUTOHSCROLL | CBS_DROPDOWNLIST , rect, &m_wndToolBar, ID_PEER_FILE_REMOTE_DROPLIST)) { TRACE0("Failed to create combo-box\n"); return ; } m_wndToolBar.m_driver.ShowWindow(SW_SHOW); } void CPeerFileRemoteListView::OnPeerFileRemoteList() { // TODO: Add your command handler code here list.ModifyStyle(style,LVS_LIST); style=LVS_LIST; } void CPeerFileRemoteListView::OnPeerFileReomteBig() { // TODO: Add your command handler code here list.ModifyStyle(style,LVS_ICON ); style=LVS_ICON; } void CPeerFileRemoteListView::OnPeerFileReomteReport() { // TODO: Add your command handler code here list.ModifyStyle(style,LVS_REPORT); style=LVS_REPORT; } void CPeerFileRemoteListView::OnPEERFILEREOMTESMAll() { // TODO: Add your command handler code here list.ModifyStyle(style,LVS_SMALLICON ); style=LVS_SMALLICON ; } void CPeerFileRemoteListView::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); // TODO: Add your message handler code here CView::OnSize(nType, cx, cy); m_wndToolBar.MoveWindow(0,0,cx,25); list.MoveWindow(0,25,cx,cy-25); } void CPeerFileRemoteListView::OnSelectChange() { m_wndToolBar.m_driver.GetLBText(m_wndToolBar.m_driver.GetCurSel(),path); AfxGetApp()->DoWaitCursor(1); Retrive(); AfxGetApp()->DoWaitCursor(0); } void CPeerFileRemoteListView::Retrive() { CString Mess; Mess+="返回目录:\""+path+"\" 的所有文件信息!"; list.DeleteAllItems(); WSAEVENT hevent=WSACreateEvent(); if(hevent==WSA_INVALID_EVENT) { Message(Mess.GetBuffer(Mess.GetLength()),"失败"); return; } SOCKET s; COMMAND com; DWORD ret; int index=0; CString nowpath; nowpath=path+"\\*"; int imagecount; imagecount=imagelists.GetImageCount(); for(int j=1;j<imagecount;j++) { imagelists.Remove(j); imagelistb.Remove(j); } CImageList* pSmallImage,* pBigImage; pSmallImage=list.GetImageList(LVSIL_SMALL); pBigImage=list.GetImageList(LVSIL_NORMAL); if(!PeerCreateSocket(&s,PEER_STREAM)) { Message(Mess.GetBuffer(Mess.GetLength()),"失败"); WSACloseEvent(hevent); return; } if(!PeerConnectSocket(s,ADDRESS,7788)) { Message(Mess.GetBuffer(Mess.GetLength()),"失败"); WSACloseEvent(hevent); closesocket(s); return; } com.CommandID=PEER_FILE_GETATTRIB; if(!PeerSendDataS(s,(char*)&com,sizeof(com),&ret,hevent)) { Message(Mess.GetBuffer(Mess.GetLength()),"失败"); WSACloseEvent(hevent); closesocket(s); return; } FILEATTRIB fileattrib; strcpy(fileattrib.filename,nowpath); if(!PeerSendDataS(s,(char*)&fileattrib,sizeof(fileattrib),&ret,hevent)) { Message(Mess.GetBuffer(Mess.GetLength()),"失败"); WSACloseEvent(hevent); closesocket(s); return; } index=list.GetItemCount(); list.InsertItem(index,".",0); list.SetItem(index,1,LVIF_TEXT,"文件夹",0,0,0,NULL); index=list.GetItemCount(); list.InsertItem(index,"..",0); list.SetItem(index,1,LVIF_TEXT,"文件夹",0,0,0,NULL); index=list.GetItemCount(); int indeximage; SHFILEINFO shfi; while(PeerRecvDataS(s,(char*)&fileattrib,sizeof(fileattrib),&ret,hevent)) { SHGetFileInfo(fileattrib.filename, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(shfi), SHGFI_ICON|SHGFI_USEFILEATTRIBUTES|SHGFI_TYPENAME ); indeximage=pSmallImage->Add(shfi.hIcon); pBigImage->Add(shfi.hIcon); index=list.GetItemCount(); list.InsertItem(index,fileattrib.filename, strcmp(fileattrib.attrib,"文件夹")==0?0:indeximage); list.SetItem(index,1,LVIF_TEXT,fileattrib.attrib,0,0,0,NULL); list.SetItem(index,2,LVIF_TEXT,fileattrib.len,0,0,0,NULL); list.SetItem(index,3,LVIF_TEXT,fileattrib.time,0,0,0,NULL); } Message(Mess.GetBuffer(Mess.GetLength()),"成功"); WSACloseEvent(hevent); closesocket(s); } void CPeerFileRemoteListView::OnRemoteLD(NMHDR* pNotifyStruct,LRESULT* result) { if(list.GetItemCount()<=0) return; LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW)pNotifyStruct; if(lpnmlv->iItem>=0) { CString filename,type; filename=list.GetItemText(lpnmlv->iItem,0); type=list.GetItemText(lpnmlv->iItem,1); ffile=filename; if(type=="文件夹") { if(filename==".") goto there; else if(filename=="..") { int findc=path.ReverseFind('\\'); if(findc==-1) goto there; path=path.Left(findc); } else { path=path+"\\"+filename; } AfxGetApp()->DoWaitCursor(1); Retrive(); AfxGetApp()->DoWaitCursor(0); } else { // ShellExecute(NULL, _T("open"), path+"\\"+filename, NULL,NULL, SW_SHOWNORMAL); CPoint point; ::GetCursorPos(&point); // ScreenToClient(&point); CMenu menu; VERIFY(menu.LoadMenu(IDR_FILE_REMOTE)); CMenu* pPopup = menu.GetSubMenu(1); ASSERT(pPopup != NULL); CWnd* pWndPopupOwner = this; while (pWndPopupOwner->GetStyle() & WS_CHILD) pWndPopupOwner = pWndPopupOwner->GetParent(); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner); } } there: *result=0; } void CPeerFileRemoteListView::OnInitialUpdate() { CView::OnInitialUpdate(); // TODO: Add your specialized code here and/or call the base class } void CPeerFileRemoteListView::OnBeginDrag(NMHDR* pNotifyStruct,LRESULT* result) { if(list.GetItemCount()<=2) return; //确信要拖动,而不是随便点击 //按下左键250ms则认为要拖动 Sleep(250); MSG msg; ::PeekMessage( &msg, GetSafeHwnd(), WM_LBUTTONUP, WM_LBUTTONUP, PM_NOREMOVE ); //随意点击而已,返回 if( msg.message==WM_LBUTTONUP ) return; DRAG Contens; Contens.flag=1; Contens.wnd=(DWORD)this; HGLOBAL hData = GlobalAlloc(GHND|GMEM_SHARE,sizeof(DRAG) ); char *pS = (char *)GlobalLock( hData ); memcpy( pS, (char*)&Contens,sizeof(DRAG)); GlobalUnlock( hData ); list.m_bDraging = TRUE; //开始拖动 COleDataSource source; source.CacheGlobalData( RegisterClipboardFormat("ListCtrl")/*CF_TEXT*/, hData ); source.DoDragDrop( DROPEFFECT_COPY ); GlobalFree( hData ); list.m_bDraging = FALSE; //完成拖动 *result=0; } void CPeerFileRemoteListView::OnLocalRC(NMHDR* pNotifyStruct,LRESULT* result) { if(list.GetItemCount()<=0) return; LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW)pNotifyStruct; CPoint point; ::GetCursorPos(&point); // ScreenToClient(&point); CMenu menu; VERIFY(menu.LoadMenu(IDR_FILE_REMOTE)); CMenu* pPopup = menu.GetSubMenu(0); ASSERT(pPopup != NULL); if(lpnmlv->iItem<0) pPopup->EnableMenuItem(IDR_FILE_REMOTE_DELFILE,MF_GRAYED); CWnd* pWndPopupOwner = this; while (pWndPopupOwner->GetStyle() & WS_CHILD) pWndPopupOwner = pWndPopupOwner->GetParent(); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner); } void CPeerFileRemoteListView::OnFileRemoteCreatedir() { // TODO: Add your command handler code here CCreateDirDlg dir(0,this); dir.DoModal(); } void CPeerFileRemoteListView::OnFileRemoteDelfile() { // TODO: Add your command handler code here if(MessageBox( "确定要删除吗?","提示",MB_YESNO)!=IDYES) return; CString filename,attrib; WSAEVENT hEvent=WSACreateEvent(); if(hEvent==WSA_INVALID_EVENT) { return; } SOCKET s; if(!PeerCreateSocket(&s,PEER_STREAM)) { WSACloseEvent(hEvent); return; } if(!PeerConnectSocket(s,ADDRESS,7788)) { WSACloseEvent(hEvent); closesocket(s); return; } DWORD retLen; FILEINFO fi; COMMAND com; com.CommandID=PEER_FILE_DELETEFILE; if(!PeerSendDataS(s,(char*)&com,sizeof(com),&retLen,hEvent)) { WSACloseEvent(hEvent); closesocket(s); return; } POSITION pos = list.GetFirstSelectedItemPosition(); int nItem; while (pos) { nItem = list.GetNextSelectedItem(pos); filename=list.GetItemText(nItem,0); attrib=list.GetItemText(nItem,1); if(filename=="." || filename=="..") continue; filename=path+"\\"+filename; strcpy(fi.filename,filename); if(attrib=="文件夹") { fi.dir=true; } else { fi.dir=false; } if(!PeerSendDataS(s,(char*)&fi,sizeof(fi),&retLen,hEvent)) { WSACloseEvent(hEvent); closesocket(s); return; } if(!PeerRecvDataS(s,(char*)&com,sizeof(com),&retLen,hEvent)) { WSACloseEvent(hEvent); closesocket(s); return; } CString Mess="删除:"+filename; if(com.CommandID==PEER_CFM_OK) Message(Mess.GetBuffer(Mess.GetLength()),"成功"); else Message(Mess.GetBuffer(Mess.GetLength()),"失败"); } Retrive(); WSACloseEvent(hEvent); closesocket(s); } void CPeerFileRemoteListView::OnFileRemoteExe() { // TODO: Add your command handler code here WSAEVENT hEvent=WSACreateEvent(); if(hEvent==WSA_INVALID_EVENT) { return; } SOCKET s; if(!PeerCreateSocket(&s,PEER_STREAM)) { WSACloseEvent(hEvent); return; } if(!PeerConnectSocket(s,ADDRESS,7788)) { WSACloseEvent(hEvent); closesocket(s); return; } DWORD retLen; FILEINFO fi; COMMAND com; com.CommandID=PEER_FILE_EXCUTE; if(!PeerSendDataS(s,(char*)&com,sizeof(com),&retLen,hEvent)) { WSACloseEvent(hEvent); closesocket(s); return; } CString file; file=path+"\\"+ffile; strcpy(fi.filename,file); if(!PeerSendDataS(s,(char*)&fi,sizeof(fi),&retLen,hEvent)) { WSACloseEvent(hEvent); closesocket(s); return; } WSACloseEvent(hEvent); CString Mess="执行:"+file; Message(Mess.GetBuffer(Mess.GetLength()),"未定"); closesocket(s); }